﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
Imports System.Windows.Controls.Primitives

Namespace PivotContent

    Partial Public Class SampleDataTablePage
        Inherits UserControl
        Public Sub New()
            Me.InitializeComponent()
        End Sub

        Public Custom Event Navigate As EventHandler(Of NavigationEventArgs)
            AddHandler(ByVal value As EventHandler(Of NavigationEventArgs))
                AddHandler Me.ViewModel.Navigate, value
            End AddHandler
            RemoveHandler(ByVal value As EventHandler(Of NavigationEventArgs))
                RemoveHandler Me.ViewModel.Navigate, value
            End RemoveHandler
            RaiseEvent(ByVal sender As System.Object, ByVal e As NavigationEventArgs)
            End RaiseEvent
        End Event

        Public Property ViewModel() As SampleDataTablePageViewModel
            Get
                Return TryCast(Me.DataContext, SampleDataTablePageViewModel)
            End Get
            Set(ByVal value As SampleDataTablePageViewModel)
                Me.DataContext = value
            End Set
        End Property

        Private Shared Sub SetApplicationState(ByVal key As String, ByVal value As Object)
            If PhoneApplicationService.Current.State.ContainsKey(key) Then
                PhoneApplicationService.Current.State.Remove(key)
            End If

            PhoneApplicationService.Current.State.Add(key, value)
        End Sub

        Private Sub OnSampleDataTablePageLoaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            If Me.ViewModel IsNot Nothing Then
                Me.ViewModel.LoadTable()
            End If
        End Sub

        Private Sub OnSelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
            If Me.ViewModel IsNot Nothing Then
                Dim selector = TryCast(sender, Selector)
                If (selector Is Nothing) OrElse (selector.SelectedIndex = -1) Then
                    Return
                End If

                SetApplicationState("CurrentSampleDataRow", Me.ViewModel.Table(selector.SelectedIndex))
                Me.ViewModel.NavigateTo(New Uri("/Pages/SampleDataDetailsPage.xaml?editSampleData=true", UriKind.Relative))
                selector.SelectedIndex = -1
            End If
        End Sub
    End Class
End Namespace